#include "xtypes.h"
#include "util_image.h"
#include "util_misc.h"

#ifndef _WIN32_WINNT		
#define _WIN32_WINNT 0x0501
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int_t GetSysProcessorNum()
{
	SYSTEM_INFO sys_info;
	GetNativeSystemInfo(&sys_info);
	return (int_t)sys_info.dwNumberOfProcessors;
}


static dword_t MyBGRA2RGB0(dword_t _Color)
{
	// 	char_t *p;
	// 	p=(char*)&_Color;
	// 	xSwapVar(p[0], p[2]);
	//	p[3] = 0;
	//	return _Color;

	__asm
	{
		lea ecx, _Color;
		mov eax, [ecx];
		mov dl, al;
		ror eax, 16;
		mov dh, al;
		mov al, dl;
		mov ah,0;	//set A=0
		ror eax, 16;
		mov al, dh;
	}	
}

void blit_from_canvas_to_dc(uint_t hDevice, CxSphereCanvas& canvas, int_t x_start, int_t y_start)
{
	HWND hWnd = (HWND)hDevice;
	HDC hMemDC;
	HBITMAP hBitmap;
	int nHeight, nWidth;
	int x,y;
	
	nWidth = canvas.GetWidth();
	nHeight = canvas.GetWidth();
	
	HDC hDestDC = GetDC(hWnd);
	
	hMemDC = CreateCompatibleDC(hDestDC);
	hBitmap = CreateCompatibleBitmap(hDestDC, nWidth, nHeight);
	SelectObject(hMemDC, hBitmap);
	
	for(y=0;y<=nHeight-1;y++)
	{
		for(x=0;x<=nWidth-1;x++)
		{
			::SetPixel(hMemDC, x, y, (MyBGRA2RGB0(canvas.GetPixel(x,y))));
		}
	}
	
	BitBlt(hDestDC, x_start, y_start, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
	
	DeleteDC(hMemDC);
	ReleaseDC(hWnd, hDestDC);
	
	
	// return success!
	return;
}





CxString Convert_MBCS_To_UNICODE( const CxString& _Str )
{
	return _Str;//	wctomb()
}
CxString Convert_UNICODE_To_MBCS( const CxString& _Str )
{
	return _Str;//	mbtowc()
}


CxString Convert_UTF8_To_UNICODE( const CxString& _Str )
{
	return _Str;
}
CxString Convert_UNICODE_To_UTF8( const CxString& _Str )
{
	return _Str;
}

CxString Convert_UTF8_To_MBCS( const CxString& _Str, uint_t _LocalCodePage )
{	
	int nSize = MultiByteToWideChar( CP_UTF8, 0, _Str.c_str(), _Str.length(), 0, 0 );
	CxStaticBuffer buf0(sizeof( wchar_t) * (nSize + 1));
	wchar_t * p = (wchar_t* )buf0.data();
	int nWideLen = MultiByteToWideChar( CP_UTF8, 0, _Str.c_str(), _Str.length(), p, nSize + 1 );

	nSize = WideCharToMultiByte( _LocalCodePage, 0, p, nWideLen, 0, 0, 0, 0 );
	CxStaticBuffer buf1(nSize);
	WideCharToMultiByte( _LocalCodePage, 0, p, nWideLen, (char*)buf1.data(), nSize, 0, 0 );
	CxString strTmp;
	strTmp.assign((char*)buf1.data());
	return strTmp;
}
CxString Convert_MBCS_To_UTF8( const CxString& _Str, uint_t _LocalCodePage )
{
	int nSize = MultiByteToWideChar( _LocalCodePage, 0, _Str.c_str(), _Str.length(), 0, 0 );
	CxStaticBuffer buf0(sizeof( wchar_t) * (nSize + 1));
	wchar_t * p = (wchar_t* )buf0.data();
	int nWideLen = MultiByteToWideChar( _LocalCodePage, 0, _Str.c_str(), _Str.length(), p, nSize + 1 );

	nSize = WideCharToMultiByte( CP_UTF8, 0, p, nWideLen, 0, 0, 0, 0 );
	CxStaticBuffer buf1(nSize);
	WideCharToMultiByte( CP_UTF8, 0, p, nWideLen, (char*)buf1.data(), nSize, 0, 0 );
	CxString strTmp;
	strTmp.assign((char*)buf1.data());
	return strTmp;
}





